package com.richapp.launcher.control;

import android.content.Context;

import com.richapp.launcher.database.system.dao.UserLoginDao;
import com.richapp.launcher.database.system.mode.UserLogin;

import java.util.List;

/**
 * Created by jack on 2015/5/8 0008.
 */
public class PhoneControl {

    private static final int volLow = 25;
    private static PhoneControl phoneControl = null;
    private CustomSysSettingManager customSysSettingManager = null;
    private Context context;
    private int lastVol = volLow;
    private int phoneCount = 0;
    private boolean musicPauseFlag = false;

    private PhoneControl(){

    }

    public static PhoneControl getInstance(){
        if(phoneControl == null){
            phoneControl = new PhoneControl();
        }
        return phoneControl;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
        if(customSysSettingManager == null){
            customSysSettingManager = new CustomSysSettingManager(context);
        }
        lastVol = customSysSettingManager.getCurrentVolume(CustomSysSettingManager.SoundType.Music);
    }

    public void phoneComming(){
        if(customSysSettingManager.isAudioOrMediaPlay()){
            if(phoneCount == 0){
                lastVol = customSysSettingManager.getCurrentVolume(CustomSysSettingManager.SoundType.Music);
                customSysSettingManager.setVolume(CustomSysSettingManager.SoundType.Music,volLow);
            }

            List<UserLogin> logins = new UserLoginDao(context).getAll();

            if(logins.size() == 1){
                if(!musicPauseFlag){
                    PageControl.getInstance().confirm();
                    musicPauseFlag = true;
                }
            }
        }
        phoneCount++;
    }

    public void phoneClosed(){
        if(phoneCount == 1) {
            customSysSettingManager.setVolume(CustomSysSettingManager.SoundType.Music,lastVol);
        }

        List<UserLogin> logins = new UserLoginDao(context).getAll();

        if(logins.size() == 1){
            if(musicPauseFlag){
                PageControl.getInstance().confirm();
                musicPauseFlag = false;
            }
        }

        if(phoneCount >= 1){
            phoneCount--;
        }
    }
}
